//
//  CYCollectionView.m
//  Demo
//
//  Created by lc4y on 2017/11/5.
//  Copyright © 2017年 lc4y. All rights reserved.
//

#import "CYCollectionView.h"
#define kScreenWidth    [UIScreen mainScreen].bounds.size.width
#define kScreenHeight   [UIScreen mainScreen].bounds.size.height

@interface CYCollectionView ()

/** 列表样式 **/
@property (nonatomic, strong) UICollectionViewLayout *cy_layout;

@end

@implementation CYCollectionView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.cy_layout = [[UICollectionViewFlowLayout alloc] init];
        self.cy_delegate = self;
    }
    
    return self;
}

- (instancetype)initWithLayout:(UICollectionViewLayout *)layout
{
    self = [super init];
    if (self) {
        self.cy_layout = layout;
        self.cy_delegate = self;
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    [self.cy_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)setCy_dataArray:(NSArray *)cy_dataArray {
    _cy_dataArray = [cy_dataArray copy];
    
    if( cy_dataArray.count ) {
        // 数据源发生变化 刷新数据
        [self cy_hiddenNoDataView];
    } else {
        [self cy_showNoDataView];
    }
    
    [self.cy_collectionView reloadData];
    
    
}

- (void)cy_hiddenNoDataView {
    if( self.cy_noDataView.superview ){
        [self.cy_noDataView removeFromSuperview];
    }
}

- (void)cy_showNoDataView {
    __weak typeof(self) weakSelf = self;
    self.cy_noDataView.cyAddToSuperView(self.cy_collectionView);
    [self.cy_noDataView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        make.width.mas_equalTo(weakSelf.cy_collectionView.mas_width);
        make.height.mas_equalTo(weakSelf.cy_collectionView.mas_height);
    }];
    
    [self.cy_noDataView cy_configNoDataImageN:self.cy_noDataImageN];
    [self.cy_noDataView cy_configNoDataTitleN:self.cy_noDataTitleN];
}

- (void)cy_registerCellClass:(NSString *)cellClassString {
    self.cy_cellID = cellClassString;
    [self.cy_collectionView registerClass:NSClassFromString(cellClassString) forCellWithReuseIdentifier:cellClassString];
}

- (void)cy_registerCellNib:(NSString *)cellNibName {
    self.cy_cellID = cellNibName;
    [self.cy_collectionView registerNib:[UINib nibWithNibName:cellNibName bundle:nil] forCellWithReuseIdentifier:cellNibName];
}

- (void)cy_registerHeaderNib:(NSString *)headerNibName {
    self.cy_headerID = headerNibName;
    [self.cy_collectionView registerNib:[UINib nibWithNibName:headerNibName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerNibName];
}

- (void)cy_registerHeaderClass:(NSString *)headerClassString {
    self.cy_headerID = headerClassString;
    [self.cy_collectionView registerClass:NSClassFromString(headerClassString) forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerClassString];
}

- (void)cy_registerFooterNib:(NSString *)footerNibName {
    self.cy_footerID = footerNibName;
    [self.cy_collectionView registerNib:[UINib nibWithNibName:footerNibName bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerNibName];
}

- (void)cy_registerFooterClass:(NSString *)footerClassString {
    self.cy_footerID = footerClassString;
    [self.cy_collectionView registerClass:NSClassFromString(footerClassString) forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerClassString];
}

- (void)cy_addHeaderRefreshControl:(HeaderRefreshCallBack)refreshCallBack {
    self.cy_collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        refreshCallBack();
    }];
}

- (void)cy_addFooterRefreshMoreControl:(FooterRefreshMoreCallBack)refreshCallBack {
    self.cy_collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        refreshCallBack();
    }];
}

- (void)cy_endRefreshControl {
    [self.cy_collectionView.mj_header endRefreshing];
    [self.cy_collectionView.mj_footer endRefreshing];
}

- (void)cy_listViewReloadView {
    [self.cy_collectionView reloadData];
}

- (kCYCustomCellID *)cy_cellIDWithIndexPath:(NSIndexPath *)indexPath { return nil; }

- (void)cy_refreshCell:(id)cell indexPath:(NSIndexPath *)indexPath {}

- (void)cy_refreshHeader:(id)headerView section:(NSInteger)section {}

- (void)cy_refreshFooter:(id)footerView section:(NSInteger)section {}

#pragma mark -
#pragma mark - collection delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.cy_eventDelegate && [self.cy_eventDelegate respondsToSelector:@selector(cy_listViewDidScroll:)]) {
        [self.cy_eventDelegate cy_listViewDidScroll:scrollView];
    }
}

//section number
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//row number
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cy_dataArray.count;
}

//cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_cellIDWithIndexPath:)]) {
        if([self.cy_delegate cy_cellIDWithIndexPath:indexPath]) {
            self.cy_cellID = [self.cy_delegate cy_cellIDWithIndexPath:indexPath];
        }
    }
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cy_cellID forIndexPath:indexPath];
    [self cy_refreshCell:cell indexPath:indexPath];
    return cell;
}

//select cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.cy_eventDelegate && [self.cy_eventDelegate respondsToSelector:@selector(cy_didSelectCellRow:)]) {
        [self.cy_eventDelegate cy_didSelectCellRow:indexPath];
    }
}

// 返回单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(40, 40);
}

// 返回每个section,单元格与上左下右的间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

// 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 最小单元格间距
// 间距会均分到每一个单元格之间，只需要满足大于此返回值就可以
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}


//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_sectionHeaderIDWithSection:)]) {
            if([self.cy_delegate cy_sectionHeaderIDWithSection:indexPath.section]) {
                self.cy_headerID = [self.cy_delegate cy_sectionHeaderIDWithSection:indexPath.section];
            }
        }
        
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                        withReuseIdentifier:self.cy_headerID forIndexPath:indexPath];
        
        [self cy_refreshHeader:headView section:indexPath.section];
        return headView;
    } else if (kind == UICollectionElementKindSectionFooter) {
        if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_sectionFooterIDWithSection:)]) {
            if([self.cy_delegate cy_sectionFooterIDWithSection:indexPath.section]) {
                self.cy_footerID = [self.cy_delegate cy_sectionFooterIDWithSection:indexPath.section];
            }
        }
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:self.cy_footerID forIndexPath:indexPath];
        [self cy_refreshFooter:footerView section:indexPath.section];
        return footerView;
    } else {
        
        return nil;
    }
}

// 设置section头视图的参考大小，与tableheaderview类似
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(0, 0);
}


#pragma mark - lazy method
- (UICollectionView *)cy_collectionView {
    if( !_cy_collectionView ) {
        _cy_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.cy_layout];
        _cy_collectionView.dataSource = self;
        _cy_collectionView.delegate = self;
        _cy_collectionView.backgroundColor = [UIColor clearColor];
        [self addSubview:_cy_collectionView];
    }
    
    return _cy_collectionView;
}

@end
